package com.whf15.o2o.service.impl;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.whf15.o2o.dao.PersonInfoDao;
import com.whf15.o2o.dao.WechatAuthDao;
import com.whf15.o2o.dto.ImageHolder;
import com.whf15.o2o.dto.WechatAuthExecution;
import com.whf15.o2o.entity.PersonInfo;
import com.whf15.o2o.entity.WechatAuth;
import com.whf15.o2o.enums.WechatAuthStateEnum;
import com.whf15.o2o.exceptions.WechatAythOperationException;
import com.whf15.o2o.service.WechatAuthService;
import com.whf15.o2o.util.FileUtil;
import com.whf15.o2o.util.ImageUtil;

@Service
public class WechatAuthServiceImpl implements WechatAuthService {
	private static Logger log = LoggerFactory
			.getLogger(WechatAuthServiceImpl.class);
	@Autowired
	private WechatAuthDao wechatAuthDao;
	@Autowired
	private PersonInfoDao personInfoDao;

	@Override
	public WechatAuth getWechatAuthByOpenId(String openId) {
		return wechatAuthDao.queryWechatInfoByOpenId(openId);
	}

	@Override
	@Transactional
	public WechatAuthExecution register(WechatAuth wechatAuth) throws WechatAythOperationException {
		//空值判断
		if (wechatAuth == null || wechatAuth.getOpenId() == null) {
			return new WechatAuthExecution(WechatAuthStateEnum.NULL_AUTH_INFO);
		}
		try {
			//设置创建时间
			wechatAuth.setCreateTime(new Date());
			//如果微信账号里夹带着用户信息并且用户Id为空，则认为该用户第一次登录
			//则自动创建用户信息
			if (wechatAuth.getPersonInfo() != null
					&& wechatAuth.getPersonInfo().getUserId() == null) {
				try {
					wechatAuth.getPersonInfo().setCreateTime(new Date());
					wechatAuth.getPersonInfo().setEnableStatus(1);
					PersonInfo personInfo = wechatAuth.getPersonInfo();
					int effectedNum = personInfoDao.insertPersonInfo(personInfo);
					if(effectedNum <= 0) {
						throw new WechatAythOperationException("添加用户信息失败");
					}	
				}catch(Exception e) {
					log.error("insertPersonInfo error:"+e.toString());
					throw new WechatAythOperationException("insertPersonInfo error:"+e.getMessage());
				}
			}
			//创建专属于本平台的微信账号
			int effectedNum = wechatAuthDao.insertWechatAuth(wechatAuth);
			if(effectedNum <= 0 ) {
				throw new WechatAythOperationException("账号创建失败");
			}else {
				return new WechatAuthExecution(WechatAuthStateEnum.SUCCESS,wechatAuth);
			}
		}catch(Exception e){
			log.error("insertPersonInfo error:"+e.toString());
			throw new WechatAythOperationException("insertPersonInfo error:"+e.getMessage());
		}
	}

	private void addProfileImg(WechatAuth wechatAuth,
			ImageHolder profileImg) {
		String dest = FileUtil.getPersonInfoImagePath();
		String profileImgAddr = ImageUtil.generateThumbnail(profileImg, dest);
		wechatAuth.getPersonInfo().setProfileImg(profileImgAddr);
	}

	/**
	* 方法描述：
	* 创建人：whfo   
	* 创建时间：2021年8月25日 上午10:48:43
	* @version 
	*/
	@Override
	public WechatAuthExecution register(WechatAuth wechatAuth, CommonsMultipartFile profileImg)
			throws RuntimeException {
		// TODO Auto-generated method stub
		return null;
	}


}
